package com.cpi.cornpp.distributionfutureversion.converter;

import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;

import com.cpi.cornpp.common.exception.CornPPException;
import com.cpi.cornpp.distributionfutureversion.dto.DistributionFutureVersionDetailDTO;
import com.cpi.cornpp.distributionfutureversion.exception.DistributionFutureVersionException;
import com.cpi.cornpp.distributionfutureversion.view.DistributionFutureVersionDetailView;
import com.cs.common.utils.NumberUtil;

public class DistributionFutureVersionDetailConverter {
	/**
	 * Balance Version DTO to Views Converter
	 * @author Edgar Joao
	 * @param dto
	 * @return
	 * @throws DistributionFutureVersionException
	 */
	public DistributionFutureVersionDetailView converterDistributionFutureVersionDetailDtoToView(DistributionFutureVersionDetailDTO dto) throws DistributionFutureVersionException{
		DistributionFutureVersionDetailView distributionFutureVersionDetailView = null;
		try{
			if(dto != null){
				distributionFutureVersionDetailView = new DistributionFutureVersionDetailView();
					distributionFutureVersionDetailView.setId(dto.getId());	
					distributionFutureVersionDetailView.setOrder(dto.getCode() + NumberUtil.fill(dto.getCodeNumber().intValue(), '0', 3));
					distributionFutureVersionDetailView.setTons(NumberUtil.convertTon(dto.getTons()));
					distributionFutureVersionDetailView.setPeriodDescription(dto.getPeriodDescription());
					distributionFutureVersionDetailView.setFuturesRequired(NumberUtil.convertInteger(dto.getFuturesRequired()));
					distributionFutureVersionDetailView.setFuturesAssigned(NumberUtil.convertInteger(dto.getFuturesAssigned()));				
					distributionFutureVersionDetailView.setBalanced(NumberUtil.convertPrice(dto.getBalanced()));
					distributionFutureVersionDetailView.setFutures(NumberUtil.convertInteger(dto.getFutures()));
					distributionFutureVersionDetailView.setPrice(NumberUtil.convertPrice(dto.getPrice()));
			}
		}catch( Exception exception ){
			DistributionFutureVersionException distributionFutureVersionDetailException=null;
			exception.printStackTrace(System.out);
			distributionFutureVersionDetailException= new DistributionFutureVersionException(exception , CornPPException.LAYER_CONVERTER,CornPPException.ACTION_LISTS);
			throw distributionFutureVersionDetailException;
		}
		return distributionFutureVersionDetailView;
	}
	
	public List convertDTOsToViews(List distributionFutureVersionDetailsDTOs) throws DistributionFutureVersionException{
		List listOfDistributionFutureVersionsViews = null;
		Iterator iterator = null;
		DistributionFutureVersionDetailDTO distributionFutureVersionDetailDTO = null;
		DistributionFutureVersionDetailView distributionFutureVersionView = null;
		try{
			listOfDistributionFutureVersionsViews = new ArrayList(0);
			iterator = distributionFutureVersionDetailsDTOs.iterator();
			while( iterator.hasNext() ){
				distributionFutureVersionDetailDTO =  (DistributionFutureVersionDetailDTO) iterator.next();
				distributionFutureVersionView = converterDistributionFutureVersionDetailDtoToView(distributionFutureVersionDetailDTO);
				if(listOfDistributionFutureVersionsViews.contains(distributionFutureVersionView)){
					distributionFutureVersionView.setRepeated(true);
				}else{
					distributionFutureVersionView.setRepeated(false);
				}
				listOfDistributionFutureVersionsViews.add(distributionFutureVersionView);
			}						
		}catch(DistributionFutureVersionException distributionFutureVersionException){
			throw distributionFutureVersionException;
		}catch( Exception exception ){
			DistributionFutureVersionException distributionFutureVersionException=null;
			exception.printStackTrace(System.out);
			distributionFutureVersionException= new DistributionFutureVersionException(exception , CornPPException.LAYER_CONVERTER,CornPPException.ACTION_LISTS);
			throw distributionFutureVersionException;
		}
		return listOfDistributionFutureVersionsViews;
	}
	
}



